<?php
	if (sizeof($_POST) == 2)
	{
		$tel = $_POST['tel'];
		$email = $_POST['email'];
		if (strlen(trim($email)) == 0)
		{
			$s = <<<EOT

<!doctype html>
<html>
	<head>
		<title>Implicit Authentication Service - Sign Up</title>
		<meta charset="utf-8">
	</head>
	<body>
		<p>Provide your information to sign up:</p>
		<form method="post" action="signUp.php" autocomplete="off">
			<table>
				<tr>
					<td>Email (required)</td>
					<td><input type="email" name="email" /></td>
				</tr>
				<tr>
					<td>Phone number</td>
					<td><input type="tel" name="tel" /></td>
				</tr>
				<tr>
					<td colspan="2" align="center" style="padding: 15px"><input type="submit" value="Submit" /></td>
				</tr>
			</table>
		</form>
		<p style="color:red">Email address is required!</p> 
	</body>

</html>

EOT;

			echo $s;
		}
		else
		{
			$mysqli = new mysqli('localhost', 'root', 'project3', 'implauth');
			if ($mysql->connect_errno)
			{
				echo 'Database connection error!';
				exit();
			}

			$res = $mysqli->query("select (select count(*) from serverstba where email like '$email') + (select count(*) from servers where email like '$email') as total");
			$ob = $res->fetch_object();
			$res->close();
			if ($ob->total == 0)
			{
				// generate and mail activation code
				$code = sha1($email . time());

				require_once "Mail.php";

				$from = "Implicit%20Authentication%20Service";
				$to = "$email";
				$subject = "Your account activation url";
				$body = "Thanks for using the Implicit Authentication Service, please click on the link below to activate your account. <br /><br /><a href=\"173.1.2.194/serverActivation.php?code=$code\">173.1.2.194/serverActivation.php?code=$code</a>";
				$host = "ssl://smtp.gmail.com";
				$port = "465";
				$username = "cs249project3";  
				$password = "project3";

				$headers = array ('From' => $from,
				  'To' => $to,
				  'Subject' => $subject,
				  'Content-type' => "text/html");
				$smtp = Mail::factory('smtp',
				  array ('host' => $host,
					'port' => $port,
					'auth' => true,
					'username' => $username,
					'password' => $password));

//////////////Uncomment the line below to actually send the email
				$mail = $smtp->send($to, $headers, $body);

				if (PEAR::isError($mail)) 
				{
					
					echo "*$email*";
					echo "The server experienced error while sending you an email. Please try again later. \n", $mail->getMessage();  
					exit();
				}
				else 
				{
									
					// insert into serverstba
					$sql = "insert into serverstba values ('$code', '$email', " . (strlen(trim($tel)) == 0 ? "null)" : "'$tel')" );
					if ($mysqli->query($sql))
					{
						// echo html
						echo "Registration successful!<br />An activation URL has been sent to your email address. Please follow that URL to activate your account.";
					}
					else
					{
						echo $sql;
						echo "\nError! Could not insert email and code into database!";
						exit();
					}
				}



			}
			else if ($ob->total == 1)
			{
				echo "This email address has been used!";
			}
			else
			{
				//log error here

			}
			
		}
	}
?>



































